<?php

function WriteBoxHeader($title)
{
	echo "<div class='main-left-box'>";
	echo "<div class='main-left-title'>$title</div>";
	echo "<div class='main-left-inner'>";
}

$mining = getdbosql('db_mining');
$defaultalgo = user()->getState('yaamp-algo');

$show_details = getparam('showdetails');

$user = getuserparam(getparam('address'));
if(!$user) return;

WriteBoxHeader("Wallet: $user->username");

$refcoin = getdbo('db_coins', $user->coinid);
if(!$refcoin)
{
	if($user->coinid != null)
		echo "<div style='color: red; padding: 10px; '>This wallet address is not valid.
			You will not receive payments using this address.</div>";

	$refcoin = getdbosql('db_coins', "symbol='BTC'");
}

echo "<table class='dataGrid2'>";

echo "<thead>";
echo "<tr>";
echo "<th></th>";
echo "<th>Name</th>";
echo "<th align=right>Immature</th>";
echo "<th align=right>Confirmed</th>";
echo "<th align=right>Total</th>";
echo "<th align=right>Value*</th>";
echo "</tr>";
echo "</thead>";

$total_pending = 0;

if($show_details)
{
	$t1 = microtime(true);

	$list = dbolist("select coinid from earnings where userid=$user->id group by coinid");
	if(!count($list))
		echo "<tr><td></td><td colspan=5><i>-none-</i></td></tr>";

	else
	{
		// sort by value
		foreach($list as $item)
		{
			$coin = getdbo('db_coins', $item['coinid']);
			if(!$coin) continue;

			$name = substr($coin->name, 0, 12);

			$confirmed = controller()->memcache->get_database_scalar("wallet_confirmed-$user->id-$coin->id",
				"select sum(amount) from earnings where status=1 and userid=$user->id and coinid=$coin->id");

			$unconfirmed = controller()->memcache->get_database_scalar("wallet_unconfirmed-$user->id-$coin->id",
				"select sum(amount) from earnings where status=0 and userid=$user->id and coinid=$coin->id");

			$total = $confirmed + $unconfirmed;
		//	$value = bitcoinvaluetoa($total * $coin->price / $refcoin->price);
			$value = bitcoinvaluetoa(yaamp_convert_amount_user($coin, $total, $user));

			$confirmed = altcoinvaluetoa($confirmed);
			$unconfirmed = altcoinvaluetoa($unconfirmed);
			$total = altcoinvaluetoa($total);

			echo "<tr class='ssrow'>";
			echo "<td width=18><img width=16 src='$coin->image'></td>";
			echo "<td><b><a href='/site/block?id=$coin->id' title='$coin->version'>$name</a></b><span style='font-size: .8em'> ($coin->algo)</span></td>";

			echo "<td align=right style='font-size: .8em;'>$unconfirmed</td>";
			echo "<td align=right style='font-size: .8em;'>$confirmed</td>";
			echo "<td align=right style='font-size: .8em;'>$total</td>";
			echo "<td align=right style='font-size: .8em;'>$value $refcoin->symbol</td>";

			echo "</tr>";
		}
	}

	$d1 = microtime(true) - $t1;
	controller()->memcache->add_monitoring_function('wallet_results-1', $d1);
}

//////////////////////////////////////////////////////////////////////////////

// $confirmed = bitcoinvaluetoa(controller()->memcache->get_database_scalar("wallet_confirmed-$user->id",
// 	"select sum(amount*price) from earnings where status=1 and userid=$user->id"))/$refcoin->price;

// $unconfirmed = bitcoinvaluetoa(controller()->memcache->get_database_scalar("wallet_unconfirmed-$user->id",
// 	"select sum(amount*price) from earnings where status=0 and userid=$user->id"))/$refcoin->price;

$confirmed = yaamp_convert_earnings_user($user, "status=1");
$unconfirmed = yaamp_convert_earnings_user($user, "status=0");

$total_unsold = bitcoinvaluetoa($confirmed + $unconfirmed);
$confirmed = $confirmed? bitcoinvaluetoa($confirmed): '';
$unconfirmed = $unconfirmed? bitcoinvaluetoa($unconfirmed): '';
//$total_usd = number_format($total_unsold*$mining->usdbtc*$refcoin->price, 3, '.', ' ');
$total_pending = bitcoinvaluetoa($total_pending);

if(!$show_details && $total_unsold > 0)
{
	echo "<tr><td colspan=6 align=right>
		<label style='font-size: .8em;'>
			<input type='checkbox' onclick='javascript:main_wallet_refresh_details()'>
			Show Details
		</label>
	</td></tr>";
}

echo "<tr class='ssrow' style='border-top: 3px solid #eee;'>";

echo "<td valign=top><img width=16 src='$refcoin->image'></td>";
echo "<td valign=top><b>";

if($refcoin->symbol == 'BTC')
	echo "$refcoin->name";
else
	echo "<a href='/site/block?id=$refcoin->id'>$refcoin->name</a>";

echo "<br><span style='font-size: .8em;'>(total pending)</span></b></td>";

echo "<td valign=top align=right style='font-size: .8em;'>$unconfirmed</td>";
echo "<td valign=top align=right style='font-size: .8em;'>$confirmed</td>";
echo "<td valign=top align=right style='font-size: .8em;'></td>";
echo "<td valign=top align=right style='font-size: .8em;'>$total_unsold $refcoin->symbol</td>";

echo "</tr>";

echo "<tr><td style='font-size: .7em;'>&nbsp;</td></tr>";

// ////////////////////////////////////////////////////////////////////////////

$balance = bitcoinvaluetoa($user->balance);
//$balance_usd = number_format($user->balance*$mining->usdbtc*$refcoin->price, 3, '.', ' ');

echo "<tr class='ssrow' style='border-top: 1px solid #eee;'>";
echo "<td><img width=16 src='$refcoin->image'></td>";
echo "<td colspan=3><b>Balance</b></td>";
echo "<td align=right style='font-size: .8em;'><b></b></td>";
echo "<td align=right style='font-size: .9em;'><b>$balance $refcoin->symbol</b></td>";
echo "</tr>";

////////////////////////////////////////////////////////////////////////////

$total_unpaid = bitcoinvaluetoa($balance + $total_unsold);
//$total_unpaid_usd = number_format($total_unpaid*$mining->usdbtc*$refcoin->price, 3, '.', ' ');

echo "<tr class='ssrow' style='border-top: 3px solid #eee;'>";
echo "<td><img width=16 src='$refcoin->image'></td>";
echo "<td colspan=3><b>Total Unpaid</b></td>";
echo "<td align=right style='font-size: .8em;'></td>";
echo "<td align=right style='font-size: .9em;'>$total_unpaid $refcoin->symbol</td>";
echo "</tr>";

////////////////////////////////////////////////////////////////////////////

$total_paid = controller()->memcache->get_database_scalar("wallet_total_paid-$user->id",
	"select sum(amount) from payouts where account_id=$user->id");

$total_paid = bitcoinvaluetoa($total_paid);
//$total_paid_usd = number_format($total_paid*$mining->usdbtc*$refcoin->price, 3, '.', ' ');

echo "<tr class='ssrow' style='border-top: 1px solid #eee;'>";
echo "<td><img width=16 src='$refcoin->image'></td>";
echo "<td colspan=3><b>Total Paid</b></td>";
echo "<td align=right style='font-size: .8em;'></td>";
echo "<td align=right style='font-size: .9em;'><a href='javascript:main_wallet_tx()'>$total_paid $refcoin->symbol</a></td>";
echo "</tr>";

////////////////////////////////////////////////////////////////////////////

//$delay = 7*24*60*60;

$total_earned = bitcoinvaluetoa($total_unsold + $balance + $total_paid);
//$total_earned_usd = number_format($total_earned*$mining->usdbtc*$refcoin->price, 3, '.', ' ');

echo "<tr class='ssrow' style='border-top: 3px solid #eee;'>";
echo "<td><img width=16 src='$refcoin->image'></td>";
echo "<td colspan=3><b>Total Earned</b></td>";
echo "<td align=right style='font-size: .8em;'></td>";
echo "<td align=right style='font-size: .9em;'>$total_earned $refcoin->symbol</td>";
echo "</tr>";

echo "</table>";

echo "</div>";

$usd = number_format($mining->usdbtc, 2, '.', ' ');
echo "<p style='font-size: .8em'>
	&nbsp;* approximate from current exchange rates<br>
	&nbsp;** bitstamp <b>$usd</b> USD/BTC
	</p>";

echo "</div><br>";

WriteBoxHeader("Last 24 Hours Payouts: $user->username");

$t = time()-24*60*60;
$list = getdbolist('db_payouts', "account_id=$user->id and time>$t order by time desc");

echo "<table  class='dataGrid2'>";

echo "<thead>";
echo "<tr>";
echo "<th align=right>Time</th>";
echo "<th align=right>Amount</th>";
echo "<th>Tx</th>";
echo "</tr>";
echo "</thead>";

$total = 0;
foreach($list as $payout)
{
	$d = datetoa2($payout->time);
	$amount = bitcoinvaluetoa($payout->amount);

	$payout_tx = substr($payout->tx, 0, 36).'...';

	echo "<tr class='ssrow'>";

	echo "<td align=right><b>$d ago</b></td>";
	echo "<td align=right><b>$amount</b></td>";

	if($user->coinid == 6)
		$txurl = "https://blockchain.info/tx/$payout->tx";
	else
		$txurl = "/explorer?id=$user->coinid&txid=$payout->tx";

	echo "<td style='font-family: monospace;'><a href='$txurl' target=_blank>$payout_tx</a></td>";
	echo "</tr>";

	$total += $payout->amount;
}

$amount = bitcoinvaluetoa($total);

echo "<tr class='ssrow'>";
echo "<td align=right>Total:</td>";
echo "<td align=right><b>$amount</b></td>";
echo "<td></td>";
echo "</tr>";

echo "</table><br>";
echo "</div>";

echo "</div><br>";






